<?php
/**
 * Created by PhpStorm.
 * User: Yieong
 * Date: 14-10-17
 * Time: 下午2:02
 */

class ComplexController extends AdminBaseController {
	
	public function channelSum(){
		$this->setTitle('渠道汇总数据');
		
		$filter = new PubFilter();
		$grid   = new PubGrid();
		
		$filter->addSpecialFilter(true)
		->addFilter('time_start', 'input', PubFunc::t('时间'), array('date' => 'day', 'value' => date('Y-m-d', strtotime('-3 day'))))
		->addFilter('time_end', 'input', PubFunc::t('至'), array('date' => 'day', 'value' => date('Y-m-d', strtotime('-1 day'))));
		
		$params = $filter->get();
		
		
		$header = ComplexService::channelSumHeader();
		$data   = ComplexService::channelSumData($params);
		
		$grid->addGrid($header['one'], $data['one'], array(
				'name'  => '数据',
				'order' => array('ctime', 'desc'),
				'fix'   => array(
						'col' => 2
				)
		));
		
		$grid->addGrid($header['two'], $data['two'], array(
				'name'  => '数据',
				'order' => array('ctime', 'desc'),
				'fix'   => array(
						'col' => 2
				)
		));
		
		$grid->addGrid($header['three'], $data['three'], array(
				'name'  => '数据',
				'order' => array('ctime', 'desc'),
				'fix'   => array(
						'col' => 3
				)
		));
		$grid->setShowType("line");
		$this->out['filter']    = $filter->show();
		$this->out['grid']      = $grid->show();
	}

    public function index(){
        $this->setTitle('综合数据报表');

        $filter = new PubFilter();
        $chart  = new PubChart();
        $grid   = new PubGrid();

        $periodOptions = array(
            'Daily'     => PubFunc::t('日')
        );
        
        $lod_special_id = $this->R('special_id');
        $lodos = $this->R('os');
        $filter->addSpecialFilter(false,2);
        $filter->addOsFilter($lod_special_id,true,2)
        ->addServerFilter($lod_special_id,$lodos,true,false,2)
            ->addFilter('period', 'select', PubFunc::t('周期'), array('options' => $periodOptions, 'value' => 'Daily'))
            ->addFilter('time_start', 'input', PubFunc::t('时间'), array('date' => 'day', 'value' => date('Y-m-d', strtotime('-7 day'))),2)
            ->addFilter('time_end', 'input', PubFunc::t('至'), array('date' => 'day', 'value' => date('Y-m-d', strtotime('-1 day'))),2)
            ->addExport();

        $params = $filter->get();
        
        $params = $this->indexCommon($params);

        $header = ComplexService::indexHeader($params);
        $data   = ComplexService::indexData($params);

        if($filter->isExport()){
            $filename = LibExcel::getFilename('综合数据报表', $params);
            LibExcel::exportCsv($header, $data, $filename);
        }else{
            $chartConfig = ComplexService::indexChart($data, $params);
            $chart->addChart('chart', $chartConfig['data'], array(
                'name'   => $chartConfig['title'],
                'config' => $chartConfig['config']
            ));

            $order = array('RecDate', 'desc');

            $grid->addGrid($header, $data, array(
                'name'  => PubFunc::t('数据'),
                'order' => $order,
                'fix'   => array(
                    'row' => true,
                    'col' => 1
                )
            ));
            
            $this->out['filter'] = $filter->show();
            $this->out['chart']  = $chart->show();
            $this->out['grid']   = $grid->show();
            $this->out['is_ana'] = 2;
        }
    }
    
    public function uniforms(){
    	$this->setTitle('单服数据汇总');
    
    	$filter = new PubFilter('pl');
    	$grid   = new PubGrid();
    
    	$filter->addOsFilter()
    	->addServerFilter(true)
    	->addFilter('time_start', 'input', PubFunc::t('时间'), array('date' => 'day', 'value' => date('Y-m-d', strtotime('-7 day'))))
    	->addFilter('time_end', 'input', PubFunc::t('至'), array('date' => 'day', 'value' => date('Y-m-d', strtotime('-1 day'))));
    
    	$params = $filter->get();

    
    	$header = ComplexService::uniforms_header($params);
    	$data   = array();
    
    	$grid->addGrid($header, $data, array(
    			'name'  => '单服数据',
    			'order' => array('ctime', 'desc')
    	));
    	
    	$this->out['filter']    = $filter->show();
    	$this->out['grid']      = $grid->show();
    }
    
    
    public function channel(){
    	$this->setTitle('渠道数据汇总');
    
    	$filter = new PubFilter('pl');
    	$grid   = new PubGrid();
    
    	$filter->addOsFilter()
    	->addServerFilter(true)
    	->addFilter('time_start', 'input', PubFunc::t('时间'), array('date' => 'day', 'value' => date('Y-m-d', strtotime('-7 day'))))
    	->addFilter('time_end', 'input', PubFunc::t('至'), array('date' => 'day', 'value' => date('Y-m-d', strtotime('-1 day'))));
    
    	$params = $filter->get();
    
    
    	$header = ComplexService::channel_header($params);
    	$data   = array();
    
    	$grid->addGrid($header, $data, array(
    			'name'  => '渠道数据',
    			'order' => array('ctime', 'desc')
    	));
    	 
    	$this->out['filter']    = $filter->show();
    	$this->out['grid']      = $grid->show();
    }

} 